<?php

namespace App\Http\Controllers;

use App\Util\Tool;
use Illuminate\Http\Request;
use App\Service\Area;

class AreaController extends BaseController
{

    /**
     * @var Area 定义对象
     */
    private $area;

    /**
     * 初始化对象属性
     */
    public function __construct()
    {
        $this->area = new Area();
    }

    /**
     * 省
     * @return \Illuminate\Http\JsonResponse
     */
    public function GetProvince()
    {
        $data = $this->area->GetProvinceAll()->toArray();
        return $this->ResultJson(1, "成功", $data);
    }

    /**
     * 市
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function GetCity(Request $request)
    {
        if (empty($request->cityId)) {
            return $this->ResultJson(0, '父id不能为空', []);
        }
        $data = $this->area->GetcityAll($request->cityId)->toArray();
        return $this->ResultJson(1, "成功", $data);
    }

    /**
     * 县
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function GetDistrict(Request $request)
    {
        if (empty($request->districtId)) {
            return $this->ResultJson(0, '父id不能为空', []);
        }
        $data = $this->area->GetDistrictAll($request->districtId)->toArray();
        return $this->ResultJson(1, "成功", $data);
    }

    public function getAllCitys()
    {
        $res = $this->area->getAllCitys()->toArray();
        return $this->ResultJson(1, "成功", $res);
    }

    /**
     *
     * 获取经纬度通过城市名称
     */
    public function getLatLngByAddress(Request $request)
    {
        $param = $request->input();
        if(empty($param['address'])){
            return $this->ResultJson(0, "地址名称不能为空", []);
        }
        $latLng =  (new Tool)->getLatLngByAddress($param['address']);
        return $this->ResultJson(1, "成功", $latLng);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 通过经纬度获取地址
     */
    public function getAddressByLatLng(Request $request)
    {
        $param = $request->input();
        if (empty($param['lng'])) {
            return $this->ResultJson(0, "经度不能为空", []);
        }
        if (empty($param['lat'])) {
            return $this->ResultJson(0, "纬度不能为空", []);
        }
        $res = (new Tool())->getAddresByLng($param);
        return $this->ResultJson(1, "成功", $res);
    }



}
